<?php

class ajaxResponse{

  public    $status         = null;       /* @var $status bool */
  public    $success        = array();    /* @var $success array */
  public    $errorCnt       = 0;          /* @var $errorCnt int */
  public    $errors         = array();    /* @var $errors array */
  public    $dbgs           = array();    /* @var $dbgs array */
  public    $more           = null;       /* @var $more string */
  public    $eval           = null;       /* @var $more string */
  private   $reportOnNull   = true;
  
//  function  __construct() {
//
//  }

  /**
   *
   * @param bool $st
   * @return AjaxResponse
   */
  function setReportOnNull($st){
    $this->reportOnNull = $st;
    return $this;
  }

  /**
   *
   * @param bool $st
   * @return AjaxResponse
   */
  function getReportOnNull(){
    return $this->reportOnNull;
  }

  /**
   * Set the status
   * @param bool $st
   * @return Response
   */
  function setStatus($st){
    $this->status = $st;
    return $this;
  }
  
  /**
   * Set the eval
   * @param bool $eval
   * @return Response
   */
  function setEval($eval){
    $this->eval = $eval;
    return $this;
  }
  
  /**
   * Retrive the status
   * @return bool
   */
  function getStatus(){
    return $this->status;
  }

   /**
   * Set the more
   * @param string $more
   * @return Response
   */
  function setMore($more){
    $this->more = $more;
    return $this;
  }

  /**
   * Retrive the more
   * @return bool
   */
  function getMore(){
    return $this->more;
  }

  /**
   * Add an error to stack
   * @param string $success
   * @return AjaxResponse
   */
  function addSuccess($success){

    if(!is_array($success)){
      $success = array($success);
    }

    foreach($success as $key=>$one){
      if(is_string($key)){
        $this->success[$key]  = $one;
      } else {
        $this->success[]      = $one;
      }
    }
    
    if(is_null($this->getStatus())){
      $this->setStatus(true);
    }

    return $this;
  }

  /**
   * Add an error to stack
   * @param string $error
   * @return AjaxResponse
   */
  function addError($error){

    if(!is_array($error)){
      $error = array($error);
    }

    foreach($error as $one){
      $this->errors[] = $one;
      $this->errorCnt++;
    }
    
    $this->setStatus(false);
    
    return $this;

  }
  
  /**
   * Add an error to stack
   * @param string $dbg
   * @return AjaxResponse
   */
  function addDbg($dbg){

    if(!is_array($dbg)){
      $dbg = array($dbg);
    }

    foreach($dbg as $one){
      $this->dbgs[] = $one;
    }
    return $this;
  }  

  function clearMore(){
    $more = $this->getMore();

    $more = preg_replace(array('/\n|\r/','/\t+/'),array('',' '), $more);

    $this->setMore($more);

    return $this;
  }

  function sendAsJson($contentType='application/json'){
    // Si on envoit par json, on clean tout ce qui a pu être mis avant
    // 
    $output = ob_get_contents();
    
    $st = ob_end_clean();
    
    if(strlen($output)>0){
      $this->addDbg($output);
    }
  
    ob_start();
    
    if($this->getMore()==null && $this->getReportOnNull()){
      $this->setMore('<div class="report" />');
    }
    $fromXhr = !isset($_SERVER['HTTP_X_REQUESTED_WITH']) ? false : $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest';

    $this->clearMore();
    
    $rep = json_encode($this);

    if(strpos($contentType,'json')){
      if(strpos($_SERVER['HTTP_ACCEPT'],'json')===FALSE){
        // On accepte pas le json
        $contentType ='text/plain';
      }
      
    }
  
    if(!empty ($contentType)){
        header("Content-Type: $contentType; charset=utf-8");
    }
    
    $output = headers_list();
    die($rep);
  }
}
